package com.app.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.app.sso.client.servlet.OnlineCounterListener;
import com.app.sso.client.servlet.UserBean;
import com.app.util.Constant;
import com.app.util.Util;

@SuppressWarnings( { "unused"})
@Aspect
@Component
public class IcesAop {

	@Pointcut("execution (* com.app.*.service..*.*(..))")
	// 切入点作用域
	private void anyMethod() {
	}// 声明一个切入点应用方法

	private Log log = LogFactory.getLog(getClass());


	@Before("anyMethod()")
	// 前置通知 应用切入点方法及参数名
	public void doBefore(JoinPoint join) throws Exception {
		HttpSession session = Util.getHttpSession();
		String classname = join.getTarget().getClass().getName();

		int[] nums = new int[1];
		UserBean user = (UserBean)session.getAttribute("CNNAME");
		
		if (user == null) {
			HttpServletRequest request = OnlineCounterListener.getHttpServletRequest();
			int num = OnlineCounterListener.clentGetServerMethod(request);
			if(num==0) {//服务器session失效 转到服务端登陆页
				//int num1 = nums[10000];
				//人为制造异常 session失效，页面js全局异常处理机制可以捕获
				user = new UserBean();
				user.setRoleID("5");
				user.setServerLogin(Constant.serverLogin);
				session.setAttribute("CNNAME", user);
				throw new RuntimeException("SeessionTimeout");
			}
		}
	}

	//@SuppressWarnings("unchecked")
	@AfterReturning(pointcut = "anyMethod()", returning = "result")
	// 获得返回值通知
	public void doAfterReturning(JoinPoint join, Object result) {
		HttpSession session = Util.getHttpSession();
		String method = join.getSignature().getName();
		String classname = join.getTarget().getClass().getName();
		classname = classname.substring(classname.lastIndexOf(".") + 1,
				classname.length());
		//可以写用户操作日志
		
	}

}
